package com.ruoyi.combat.service.userlocation.impl;

import java.util.*;

import com.ruoyi.combat.domain.userlocation.CombatBlog;
import com.ruoyi.combat.domain.userlocation.CombatRelation;
import com.ruoyi.combat.domain.userlocation.CombatUser;
import com.ruoyi.combat.domain.userlocation.CombatWarn;
import com.ruoyi.combat.mapper.userlocation.CombatBlogMapper;
import com.ruoyi.combat.mapper.userlocation.CombatRelationMapper;
import com.ruoyi.combat.mapper.userlocation.CombatUserMapper;
import com.ruoyi.combat.mapper.userlocation.CombatWarnMapper;
import com.ruoyi.combat.service.userlocation.ICombatBlogService;
import com.ruoyi.combat.util.CoordinateUtils;
import com.ruoyi.combat.util.MethodUseUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 用户发布内容Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-30
 */
@Service
public class CombatBlogServiceImpl implements ICombatBlogService {
    @Autowired
    private CombatBlogMapper combatBlogMapper;

    @Autowired
    private MethodUseUtils methodUseUtils;

    @Autowired
    private CombatUserMapper combatUserMapper;

    @Autowired
    private CombatWarnMapper combatWarnMapper;
    @Autowired
    private CombatRelationMapper combatRelationMapper;

    /**
     * 查询用户发布内容
     *
     * @param id 用户发布内容主键
     * @return 用户发布内容
     */
    @Override
    public CombatBlog selectCombatBlogById(Long id) {
        return combatBlogMapper.selectCombatBlogById(id);
    }

    @Override
    public CombatBlog selectCombatBlogByBlogId(String blogId) {
        return combatBlogMapper.selectCombatBlogByBlogId(blogId);
    }

    /**
     * 查询用户发布内容列表
     *
     * @param combatBlog 用户发布内容
     * @return 用户发布内容
     */
    @Override
    public List<CombatBlog> selectCombatBlogList(CombatBlog combatBlog) {
        return combatBlogMapper.selectCombatBlogList(combatBlog);
    }

    /**
     * 新增用户发布内容
     *
     * @param combatBlog 用户发布内容
     * @return 结果
     */
    @Override
    public int insertCombatBlog(CombatBlog combatBlog) {
        int num = combatBlogMapper.insertCombatBlog(combatBlog);
        try {
            judgeBlogLoactionWarn(1L, combatBlog);
        } catch (Exception e) {
            e.getMessage();
        }
        return num;
    }

    /**
     * 修改用户发布内容
     *
     * @param combatBlog 用户发布内容
     * @return 结果
     */
    @Override
    public int updateCombatBlog(CombatBlog combatBlog) {
        return combatBlogMapper.updateCombatBlog(combatBlog);
    }

    /**
     * 批量删除用户发布内容
     *
     * @param ids 需要删除的用户发布内容主键
     * @return 结果
     */
    @Override
    public int deleteCombatBlogByIds(Long[] ids) {
        return combatBlogMapper.deleteCombatBlogByIds(ids);
    }

    /**
     * 删除用户发布内容信息
     *
     * @param id 用户发布内容主键
     * @return 结果
     */
    @Override
    public int deleteCombatBlogById(Long id) {
        return combatBlogMapper.deleteCombatBlogById(id);
    }

    /**
     * 删除用户发布内容通过userid
     *
     * @param userId 用户发布内容主键
     * @return 结果
     */
    @Override
    public int deleteCombatBlogByUserId(String userId) {
        CombatRelation relation = new CombatRelation();
        relation.setUserid(userId);
        List<CombatRelation> combatRelations = combatRelationMapper.selectRelationByUserId(relation);
        for (CombatRelation rel : combatRelations){
            combatRelationMapper.deleteCombatRelationById(rel.getId());
        }


        return combatBlogMapper.deleteCombatBlogByUserId(userId);
    }

    @Override
    public List<CombatBlog> getBlogsByUserId(String userId) {
        List<CombatBlog> blogsByUserId = combatBlogMapper.getBlogsByUserId(userId);
        for (CombatBlog blog : blogsByUserId) {
            String reportImages = blog.getReportImages();
            if (StringUtils.isNotEmpty(reportImages) && !reportImages.contains("combat")) {
                String[] split = reportImages.split(",");
                String path = "";
                Date reportime = blog.getReportime();
                String data = DateUtils.parseDateToStr("yyyyMMdd", reportime);
                for (int i = 0; i < split.length; i++) {
                    int n = i + 1;
                    if (StringUtils.isNotEmpty(path)) {
                        path = path + ",/combat/" + data + " _" + blog.getId() + "_" + n + ".jpg";
                    } else {
                        path = "/combat/" + data + " _" + blog.getId() + "_" + n + ".jpg";
                    }
                    if (split.length == 1) {
                        path = "/combat/" + data + " _" + blog.getId() + ".jpg";
                    }
                }
                blog.setReportImages(path);
            }
        }
        return blogsByUserId;
    }

    @Override
    public void judgeBlogLoactionWarn(Long methodId, CombatBlog blog) {
        Map<String, Object> requestBody = new HashMap<>();
        List<String> contents = new ArrayList<>();
        contents.add(blog.getReportContent());
        requestBody.put("json", contents);
        Map<String, Object> result = methodUseUtils.methodUser(methodId, requestBody);
        if (result != null && result.containsKey("content")) {
            Map<String, Object> content = (Map<String, Object>) result.get("content");
            if (content.containsKey("coordinate")) {
                Map<String, Float> coordinate = (Map<String, Float>) content.get("coordinate");
                Float loclat = coordinate.get("lat");
                Float loclon = coordinate.get("lon");
                String province = content.get("province").toString();
                String city = content.get("city-district").toString();

                CombatUser combatUser = combatUserMapper.selectCombatUserById(Long.parseLong(blog.getUserId()));
                if (combatUser != null) {
                    long homeDistance = CoordinateUtils.calculateGEODistance(loclat, loclon,
                            Float.parseFloat(combatUser.getUserHomeLat()), Float.parseFloat(combatUser.getUserHomeLon()));
                    long workDistance = CoordinateUtils.calculateGEODistance(loclat, loclon,
                            Float.parseFloat(combatUser.getUserWorkLat()), Float.parseFloat(combatUser.getUserWorkLon()));
                    if (homeDistance < 30L) {
                        CombatWarn combatWarn = new CombatWarn(blog.getUserId(),
                                blog.getUsername(),
                                "",
                                blog.getBlogId(),
                                String.valueOf(loclat),
                                String.valueOf(loclon),
                                "家庭住址",
                                province,
                                city, "1");
                        combatWarn.setCreateTime(new Date());
                        combatWarn.setWarnState("0");
                        combatWarnMapper.insertCombatWarn(combatWarn);
                    }
                    if (workDistance < 30L) {
                        CombatWarn combatWarn = new CombatWarn(blog.getUserId(),
                                blog.getUsername(),
                                "",
                                blog.getBlogId(),
                                String.valueOf(loclat),
                                String.valueOf(loclon),
                                "工作地址",
                                province,
                                city, "1");
                        combatWarn.setCreateTime(new Date());
                        combatWarn.setWarnState("0");
                        combatWarnMapper.insertCombatWarn(combatWarn);
                    }
                }
            }
        }
    }

    @Override
    public List<Map> sendStatistics(String userId) {
        return combatBlogMapper.sendStatistics(userId);
    }

    @Override
    public Map numberStatistics(String userId) {
        CombatBlog combatBlog = new CombatBlog();
        combatBlog.setUserId(userId);
        List<CombatBlog> combatBlogs = combatBlogMapper.selectCombatBlogList(combatBlog);
        CombatUser combatUser = combatUserMapper.selectCombatUserByUserId(userId);
        Map<String, Long> result = new HashMap<>();
        result.put("userFansNum", combatUser.getUserFansNum());
        result.put("userFocusNum", combatUser.getUserFocusNum());
        result.put("userBlogNum", (long) combatBlogs.size());
        return result;
    }
}

